package com.taotao.portal.service.impl;

import com.taotao.common.HttpUtil;
import com.taotao.common.JsonUtils;
import com.taotao.common.TaotaoResult;
import com.taotao.pojo.TbItem;
import com.taotao.pojo.TbItemDesc;
import com.taotao.pojo.TbItemParamItem;
import com.taotao.portal.pojo.TbItemExt;
import com.taotao.portal.service.TbItemDetailsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class TbItemDetailsServiceImpl implements TbItemDetailsService {

    @Value("${rest.url}")
    private String restUrl;

    @Override
    public TbItemExt getTbItemInfo(Long id) {

        String resultJson = HttpUtil.doGet(restUrl + "/item/get_item_detail/" + id);
        TaotaoResult taotaoResult = TaotaoResult.formatToPojo(resultJson, TbItem.class);
        if (taotaoResult.getStatus().equals(200)) {
            TbItem tbItem = (TbItem) taotaoResult.getData();
            return parseTbItem2Ext(tbItem, new TbItemExt());
        }
        log.error("获取商品详情失败: " + taotaoResult.getMsg() + "\n异常栈信息：" + taotaoResult.getData());
        return null;
    }

    /**
     * 将TbItem对象转换成前端页面需要的TbItemExt对象
     *
     * @param src
     * @param dst
     * @return
     */
    private TbItemExt parseTbItem2Ext(TbItem src, TbItemExt dst) {
        dst.setId(src.getId());
        dst.setCreated(src.getCreated());
        dst.setUpdated(src.getUpdated());
        dst.setStatus(src.getStatus());
        dst.setBarcode(src.getBarcode());
        dst.setCid(src.getCid());
        dst.setImage(src.getImage());
        dst.setPrice(src.getPrice());
        dst.setNum(src.getNum());
        dst.setTitle(src.getTitle());
        dst.setSellPoint(src.getSellPoint());
        return dst;
    }

    @Override
    public String getTbItemDesc(Long id) {
        String jsonResult = HttpUtil.doGet(restUrl + "/item/desc/"+id);
        TaotaoResult taotaoResult = TaotaoResult.formatToPojo(jsonResult, TbItemDesc.class);
        if(taotaoResult.getStatus().equals(200)) {
            TbItemDesc itemDesc = (TbItemDesc) taotaoResult.getData();
            return itemDesc.getItemDesc();
        } else {
            log.error("请求商品描述信息出错: " + taotaoResult.getMsg()+"\n异常：" + taotaoResult.getData());
            return "<span>暂无描述</span>";
        }

    }

    @Override
    public String getItemParam(Long id) {
        String jsonResult = HttpUtil.doGet(restUrl + "/item/param/" + id);
        TaotaoResult taotaoResult = TaotaoResult.formatToPojo(jsonResult, TbItemParamItem.class);
        if (taotaoResult.getStatus().equals(200)) {
            TbItemParamItem itemParam = (TbItemParamItem) taotaoResult.getData();
            String html = buildHtmlFromTbItemParamItem2(itemParam);
            return html;
        }
        log.error("获取商品规格参数失败: " + taotaoResult.getMsg());
        return "<span>暂无商品规格参数<span>";
    }

    private String buildHtmlFromTbItemParamItem2(TbItemParamItem itemParamItem) {
        StringBuilder sb = new StringBuilder();
        sb.append("<table cellpadding=\"0\" cellspacing=\"1\" width=\"100%\" border=\"0\" class=\"Ptable\">");
        sb.append("    <tbody>");
        // 把数据库中存储的商品规格参数字符串转换成Java对象，方便下面组装html
        List<Map> paramDataItemList = JsonUtils.jsonToList(itemParamItem.getParamData(), Map.class);
        // 第一层循环是拼一个规格参数主体块的html
        for(Map itemParamMap : paramDataItemList) {
            String groupName = (String) itemParamMap.get("group");
            sb.append("        <tr>");
            sb.append("            <th class=\"tdTitle\" colspan=\"2\">"+groupName+"</th>");
            sb.append("        </tr>");
            List<Map> params = (List<Map>) itemParamMap.get("params");
            // 第二层循环是拼接一个规格参数主体下面每一项规格参数的html
            for (Map map2 : params) {
                sb.append("        <tr>");
                sb.append("            <td class=\"tdTitle\">"+map2.get("k")+"</td>");
                sb.append("            <td>"+map2.get("v")+"</td>");
                sb.append("        </tr>");
            }
        }
        sb.append("    </tbody>");
        sb.append("</table>");
        return sb.toString();
    }


}
